def prime(num):
    for i in range(2, num):
        if num % i == 0:
            return False
    return True

def max_prime(num):
    if num <= 999 or num > 9999:
        return 0
    z = num // 1000
    x = (num % 1000) // 100
    c = (num % 100) // 10
    e = num % 10
    max_ch = 0
    for ch in (num, z, x, c, e, z * 10 + x, x * 10 + c, c * 10 + e, z * 100 + x * 10 + c, x * 100 + c * 10 + e):
        if prime(ch) and ch > max_ch:
            max_ch = ch
    return max_ch

num = int(input())
result = max_prime(num)
print(result)
